﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using SomeRandomGame.Graphics;
using SomeRandomGame.Interfaces;
using SomeRandomGame.LevelTypes;
using SomeRandomGame.Utility;

namespace SomeRandomGame
{
    public class ContentFactory: IContentFactory
    {
        private readonly IGameManager _gameManager;
        private SpriteFont _gameFont;
        private SpriteFont _menuFont;
        private SoundEffect _menuBackgroundMusic;
        private Texture2D _gradientTexture;
        private SoundEffect _laserSound;

        public ContentFactory( IGameManager gameManager )
        {
            _gameManager = gameManager;
        }

        public SpriteFont GameFont
        {
            get
            {
                if( _gameFont == null )
                {
                    _gameFont = _gameManager.Game.Content.Load<SpriteFont>( Constants.Content.Fonts.GAME_FONT );
                }

                return _gameFont;
            }
        }

        public SpriteFont MenuFont
        {
            get
            {
                if( _menuFont == null )
                {
                    _menuFont = _gameManager.Game.Content.Load<SpriteFont>( Constants.Content.Fonts.MENU_FONT );
                }

                return _menuFont;
            }
        }

        public SoundEffect MenuBackgroundMusic
        {
            get
            {
                if (_menuBackgroundMusic == null)
                {
                    _menuBackgroundMusic = _gameManager.Game.Content.Load<SoundEffect>(Constants.Content.Songs.MENU_MUSIC);
                }

                return _menuBackgroundMusic;
            }
        }

        public SoundEffect LaserSound 
        { 
            get
            {
                if (_laserSound == null)
                {
                    _laserSound =
                        _gameManager.Game.Content.Load<SoundEffect>(Constants.Content.Sounds.LASER_SOUND);
                }

                return _laserSound;
            } 
        }

        public Texture2D GradientTexture
        {
            get
            {
                if( _gradientTexture == null )
                {
                    _gradientTexture =
                        _gameManager.Game.Content.Load<Texture2D>( Constants.Content.Graphics.GRADIENT_TEXTURE );
                }

                return _gradientTexture;
            }
        }

        public Texture2D GetTexture(string identifier)
        {
            if (identifier.Equals(Constants.Content.Particles.SHIP_MOTOR_PARTICLE))
            {
                return _gameManager.Game.Content.Load<Texture2D>(Constants.Content.Particles.SHIP_MOTOR_PARTICLE);
            }
            if (identifier.Equals(Constants.Content.Particles.LASER_PARTICLE))
            {
                return _gameManager.Game.Content.Load<Texture2D>(Constants.Content.Particles.LASER_PARTICLE);
            }
            return null;
        }

        // TODO: Load from some sort of file
        public IDrawableObject Get( string identifier )
        {
            if( identifier.Equals( Constants.Content.Animations.STANDARD_PLAYER_SHIP ) )
            {
                return new FrameBasedAnimation(
                    _gameManager.Game.Content.Load<Texture2D>( Constants.Content.Animations.STANDARD_PLAYER_SHIP ),
                    Vector2.Zero, 115, 69, 8, 30, Color.White, 1f, true );

            }
            else if( identifier.Equals( Constants.Content.Levels.ONE ) )
            {
                Background background = new Background( _gameManager.Game.GraphicsDevice.Viewport.Width, _gameManager.Game.GraphicsDevice.Viewport.Height, new List<IDrawableObject>()
                                                    {
                                                        new StaticBackgroundLayer(
                                                            Constants.Content.Graphics.MAIN_BACKGROUND ),
                                                        new ParallaxingBackgroundLayer( -1, _gameManager.Game.GraphicsDevice.Viewport.Width,
                                                                                        Constants.Content.Graphics.
                                                                                            BG_LAYER_1 ),
                                                        new ParallaxingBackgroundLayer( -2, _gameManager.Game.GraphicsDevice.Viewport.Width,
                                                                                        Constants.Content.Graphics.
                                                                                            BG_LAYER_2 )
                                                    } );

                return new SideScrollingLevel( background );
            }

            return null;
        }
    }
}
